package com.ellricky.teacher.ui.mycourse.workjoin

import android.view.View
import com.andr.common.http.callback.JOCallback
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.EmptyUtil
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.entity.IdNamePickerEntity
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.dialog.picker.EPickerEntityListener
import com.ellricky.baselib.dialog.picker.EPickerType
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.teacher.API_WORK_JOIN_SAVE
import com.ellricky.teacher.R
import kotlinx.android.synthetic.main.tc_activity_leave_apply.btnSubmit
import kotlinx.android.synthetic.main.tc_activity_work_join_create.*
import org.json.JSONObject

class WorkJoinCreateActivity : BaseToolBarActivity<Void, BasePresenter<Void>>(),EPickerEntityListener<IdNamePickerEntity> {


    private lateinit var ePickerUtil: EPickerUtil
    private var selPerson: IdNamePickerEntity? = null


    override fun getContentLayout(): Int {
        return R.layout.tc_activity_work_join_create
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = getString(R.string.tc_work_join_create)

        ePickerUtil = EPickerUtil(this)
        etPerson.setOnClickListener(this)
        btnSubmit.setOnClickListener(this)
    }


    override fun onClickView(v: View?) {
        super.onClickView(v)
        when (v) {
            etPerson -> ePickerUtil.checkPersonPicker(selPerson, this)
            btnSubmit -> postSubmit()
        }
    }


    override fun onSelectItem(pickType: Int, t: IdNamePickerEntity?) {
        val name = t?.name
        when (pickType) {
            EPickerType.PICK_ALL_PERSON -> {
                selPerson = t
                etPerson.value = name
            }
        }
    }


    private fun postSubmit() {
        val person = etPerson.value
        if (EmptyUtil.isEmpty(person)) {
            ToastUtil.show(this, R.string.tc_work_join_from_select)
            return
        }

        val content = etContent.value
        if (EmptyUtil.isEmpty(content)) {
            ToastUtil.show(this, R.string.tc_work_join_content_input)
            return
        }

        val paramMap = mutableMapOf<String, Any>(
                "changeTeacherId" to "${selPerson?.id}",
                "workInfo" to content
        )


        val url = EAppUtil.getApiHost() + API_WORK_JOIN_SAVE
        HttpUtils.getInstance().post(url, paramMap, object : JOCallback(this) {
            override fun onSuccess(dataJO: JSONObject?, msg: String?) {
                setResult(BaseConstant.RESULT_CODE_ADD)
                finish()
            }
        })

    }
}

